package org.ielse.frame.util;

import java.util.Set;

import org.ielse.frame.App;
import org.ielse.frame.C;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class SpfsUtil {

	public static void write(String key, Object value) {
		SharedPreferences spfs = App.i().getSharedPreferences(C.settings.SPFS_NAME, Context.MODE_PRIVATE);
		Editor editor = spfs.edit();
		if (value instanceof String) {
			editor.putString(key, (String) value);
		} else if (value instanceof Integer) {
			editor.putInt(key, (Integer) value);
		} else if (value instanceof Boolean) {
			editor.putBoolean(key, (Boolean) value);
		} else if (value instanceof Float) {
			editor.putFloat(key, (Float) value);
		} else if (value instanceof Long) {
			editor.putLong(key, (Long) value);
		} else {
			throw new IllegalArgumentException("not support type");
		}
		editor.commit();
	}

	public static String readString(String key) {
		return readString(key, null);
	}

	public static String readString(String key, String defValue) {
		SharedPreferences spfs = App.i().getSharedPreferences(C.settings.SPFS_NAME, Context.MODE_PRIVATE);
		return spfs.getString(key, defValue);
	}

	public static int readInt(String key) {
		return readInt(key, 0);
	}

	public static int readInt(String key, int defValue) {
		SharedPreferences spfs = App.i().getSharedPreferences(C.settings.SPFS_NAME, Context.MODE_PRIVATE);
		return spfs.getInt(key, defValue);
	}

	public static float readFloat(String key) {
		return readFloat(key, 0f);
	}

	public static float readFloat(String key, float defValue) {
		SharedPreferences spfs = App.i().getSharedPreferences(C.settings.SPFS_NAME, Context.MODE_PRIVATE);
		return spfs.getFloat(key, defValue);
	}

	public static boolean readBoolean(String key) {
		return readBoolean(key, false);
	}

	public static boolean readBoolean(String key, boolean defValue) {
		SharedPreferences spfs = App.i().getSharedPreferences(C.settings.SPFS_NAME, Context.MODE_PRIVATE);
		return spfs.getBoolean(key, defValue);
	}

	public static long readLong(String key) {
		return readLong(key, 0l);
	}

	public static long readLong(String key, long defValue) {
		SharedPreferences spfs = App.i().getSharedPreferences(C.settings.SPFS_NAME, Context.MODE_PRIVATE);
		return spfs.getLong(key, defValue);
	}

	public static Set<String> readStringSet(String key) {
		return readStringSet(key, null);
	}

	public static Set<String> readStringSet(String key, Set<String> defValues) {
		SharedPreferences spfs = App.i().getSharedPreferences(C.settings.SPFS_NAME, Context.MODE_PRIVATE);
		return spfs.getStringSet(key, defValues);
	}

	public static boolean contains(String key) {
		SharedPreferences spfs = App.i().getSharedPreferences(C.settings.SPFS_NAME, Context.MODE_PRIVATE);
		return spfs.contains(key);
	}

	public static boolean remove(String key) {
		SharedPreferences spfs = App.i().getSharedPreferences(C.settings.SPFS_NAME, Context.MODE_PRIVATE);
		return spfs.edit().remove(key).commit();
	}

	public boolean clear() {
		SharedPreferences spfs = App.i().getSharedPreferences(C.settings.SPFS_NAME, Context.MODE_PRIVATE);
		return spfs.edit().clear().commit();
	}
}
